local HY = require "packages.touhou_hy.utility.utility"
local U = require "packages.utility.utility"
local extension = Package:new("touhou_standard")
extension.extensionName = "touhou_hy"

Fk:loadTranslationTable {
    ["touhou_standard"] = "基础包",
}

--#region Init
local remove_card_List = {
    "double_swords",
    "blade",
}

local remove_cards = fk.CreateSkill {
    name = "remove_cards",
}
remove_cards:addEffect(fk.GamePrepared, {
    mute = true,
    global = true,
    can_trigger = function(self, event, target, player, data)
        return HY.CheckEnabled(player.room)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player)
        local room = Fk:currentRoom()
        for _, id in ipairs(Fk:getAllCardIds()) do
            if table.contains(remove_card_List, Fk:getCardById(id, true).name) then
                table.removeOne(room.draw_pile, id)
                room:setCardArea(id, Card.Void, nil)
                table.insertIfNeed(room.void, id)
                room.card_place[id] = Card.Void
            end
        end
    end
})
remove_cards:addEffect(fk.BeforeCardsMove, {
    mute = true,
    global = true,
    can_trigger = function(self, event, target, player, data)
        if not HY.CheckEnabled(player.room) then
            return false
        end
        return table.any(data, function(move)
            return table.any(move.moveInfo, function(info)
                return table.contains(remove_card_List, Fk:getCardById(info.cardId, true).name)
            end)
        end)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player)
        return true
    end
})

local remove_general_list = {
    "mouxusheng",
    "hy_support",
}

local remove_general = fk.CreateSkill {
    name = "remove_general",
}
remove_general:addEffect(fk.GamePrepared, {
    global = true,
    mute = true,
    can_refresh = function(self, event, target, player)
        if not HY.CheckEnabled(player.room) then
            return false
        end
        if #table.filter(player.room.players, function(t)
                return table.contains(remove_general_list, t.general) and t.id > 0
            end) == 0 then
            return false
        end
        return #table.filter(player.room.players, function(t, _, _)
            return table.contains(remove_general_list, t.general)
        end) > 0 and not event:getCostData(self)
    end,
    on_refresh = function(self, event, target, player)
        event:setCostData(self, true)
        local data = { "remove_test_active", "remove_test_prompt", true, json.encode({}) }
        local jsonData = json.encode(data)
        local players = table.filter(player.room.players, function(t)
            return not table.contains(remove_general_list, t.general) and t.id > 0
        end)
        if #players == 0 then
            player.room.tag["remove_test"] = false
            return
        end
        local request = Request:new(players, "AskForUseActiveSkill", 1)
        request.focus_text = ""
        request.send_encode = false
        request.receive_decode = false
        for _, p in ipairs(players) do
            request:setData(p, jsonData or p.request_data)
        end
        request:ask()
        local result = request.winners[1]
        if result then
            player.room.tag["remove_test"] = true
            for _, t in ipairs(player.room.players) do
                if table.contains(remove_general_list, t.general) then
                    player.room:killPlayer({
                        who = t
                    })
                end
            end
        else
            player.room.tag["remove_test"] = false
        end
    end
})
remove_general:addEffect(fk.AfterPlayerRevived, {
    global = true,
    mute = true,
    can_refresh = function(self, event, target, player)
        if not HY.CheckEnabled(player.room) then
            return false
        end
        if #table.filter(player.room.players, function(t)
                return table.contains(remove_general_list, t.general) and t.id > 0
            end) == 0 then
            return false
        end
        return player.room.tag["remove_test"]
    end,
    on_refresh = function(self, event, target, player)
        for _, t in ipairs(player.room.players) do
            if table.contains(remove_general_list, t.general) then
                player.room:killPlayer({
                    who = t
                })
            end
        end
    end
})

local remove_general_active = fk.CreateSkill {
    name = "remove_general_active",
}
remove_general_active:addEffect("active", {
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
})

Fk:loadTranslationTable {
    ["remove_test_prompt"] = "是否移除测试武将？",
}

local chooseCardsSkillTemp = fk.CreateSkill {
    name = "choose_cards_skill_temp",
}
chooseCardsSkillTemp:addEffect("active", {
    expand_pile = function(self)
        return Self:getTableMark("#tempMove")
    end,
    card_filter = function(self, player, to_select, selected)
        if #selected >= self.num then
            return false
        end

        if Fk:currentRoom():getCardArea(to_select) == Card.PlayerSpecial then
            if not string.find(self.pattern or "", type(self.expand_pile) == "string" and self.expand_pile or "") then return false end
        end

        local checkpoint = true
        local card = Fk:getCardById(to_select)

        if not self.include_equip then
            checkpoint = checkpoint and (Fk:currentRoom():getCardArea(to_select) ~= Player.Equip)
        end

        if self.pattern and self.pattern ~= "" then
            checkpoint = checkpoint and (Exppattern:Parse(self.pattern):match(card))
        end
        return checkpoint
    end,
    min_card_num = function(self) return self.min_num end,
    max_card_num = function(self) return self.num end,
})

Fk:loadTranslationTable {
    ["choose_cards_skill_temp"] = "",
    ["#tempMove"] = "演出效果",
}

extension:loadSkillSkels { remove_cards, remove_general, remove_general_active, chooseCardsSkillTemp }
--#endregion

local function loadGeneral(name)
    require("packages.touhou_hy.pkgs.standard.generals." .. name)(extension, HY, U)
end

--#region General Chi
loadGeneral("hy_remilia_scarlet")
loadGeneral("hy_komeiji_koishi")
loadGeneral("hy_kijin_seija")
loadGeneral("hy_sukuna_shinmyoumaru")
loadGeneral("hy_yorigami_shion")
loadGeneral("hy_yorigami_jyoon")
loadGeneral("hy_kitcho_yachie")
loadGeneral("hy_toutetsu_yuma")
--#endregion

--#region General Ten
loadGeneral("hy_tenkyu_chimata")
loadGeneral("hy_kagiyama_hina")
loadGeneral("hy_komeiji_satori")
loadGeneral("hy_yagokoro_eirin")
loadGeneral("hy_toyosatomimi_no_miko")
loadGeneral("hy_shikieiki_yamazanadou")
loadGeneral("hy_yakumo_yukari")
loadGeneral("hy_hakurei_reimu")
--#endregion

--#region General Jin
loadGeneral("hy_motoori_kosuzu")
loadGeneral("hy_murasa_minamitsu")
loadGeneral("hy_kumoi_ichirin_and_unzan")
loadGeneral("hy_kawashiro_nitori")
loadGeneral("hy_fujiwara_no_mokou")
loadGeneral("hy_rumia")
loadGeneral("hy_lilywhite")
loadGeneral("hy_kasodani_kyouko")
--#endregion

return extension
